#include <functional>
#include <string>
#include <iostream>
class Connection;
class TcpServer;

using func_t = std::function<void(Connection *)>;

class Connection
{
public:
    Connection(int sockfd, uint32_t events, TcpServer *R)
        : _sockfd(sockfd),
          _events(events),
          _R(R)
    {
    }

    void RegisterCallback(func_t recver, func_t sender, func_t excepter)
    {
        _recver = recver;
        _sender = sender;
        _excepter = excepter;
    }

    void AddInbuffer(std::string buffer)
    {
        _inbuffer += buffer;// 追加到_inbuffer
    }

    void AddOutbuffer(std::string buffer)
    {
        _outbuffer += buffer;// 追加到_inbuffer
    }

    int SockFd()
    {
        return _sockfd;
    }

    uint32_t Events()
    {
        return _events;
    }

    void SetClient(const struct sockaddr_in c)
    {
        _client = c;
    }

    std::string &Inbuffer()
    {
        return _inbuffer;
    }

    std::string &Outbuffer()
    {
        return _outbuffer;
    }

    bool OutBufferEmpty()
    {
        return _outbuffer.empty();
    }

    void SetEvents(uint32_t events)
    {
        _events = events;
    }

    void Close()
    {
        close(_sockfd);
    }
    ~Connection()
    {
    }

private:
    int _sockfd;
    std::string _inbuffer;  // 接受缓冲区
    std::string _outbuffer; // 发送缓冲区

    // 关心的事件
    uint32_t _events;

    // 维护client端的ip和port信息
    struct sockaddr_in _client;

public:
    // 回调方法
    func_t _recver;
    func_t _sender;
    func_t _excepter;
    // TcpServer的回指指针 - TODO
    TcpServer *_R;
};